package com.project.frba.bi.descriptor;

import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import com.project.frba.bi.BiUtil;

public class Property{
	
	private String name;
	private String table;
	private String column;
	private String function;
	private String label;
	private String defaultValue;
	private Class type;
	private Dimension dimension;

		
	public Property(String name, Dimension dimension){
		this.name = name;
		this.dimension = dimension;
	}


	public void setColumn(String column) {
		this.column = column;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public String getName() {
		return name;
	}

	public void setName(String propertyName) {
		this.name = propertyName;
	}

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}
	
	public String getTable(){
		return table;
	}
	
	public String getColumn(){
		return column;
	}
	
	public String getAbsolutePath(){
		String alias = this.getDimension().getJoinName(this.getTable());
		return alias + "." + this.getColumn();
	}
	
	public String getOlapAbsolutePath(){
		return this.dimension.getOlapAliasTable() + "." + this.getName();
	}
	
	public String getAlias(){
		return " AS " + this.getName();
	}


	public Dimension getDimension() {
		return dimension;
	}


	public void setDimension(Dimension dimension) {
		this.dimension = dimension;
	}

	public Class getType() {
		return type;
	}
	
	
	public void setType(Class type) {
		this.type = type;
	}
	
	public void setType(String typeName){
		this.setType(BiUtil.getPropertyType(typeName));
	}
	
	public Node getMyNode() throws XPathExpressionException{
		return this.dimension.getDescriptor().getNode("//property[@name = '" +  this.getName()+ "']");
	}


	public void loadProperty() throws XPathExpressionException {
		Node propertyNode = this.getMyNode();
		NamedNodeMap attributes = propertyNode.getAttributes();
		this.setColumn(attributes.getNamedItem("column").getNodeValue());
		this.setTable(attributes.getNamedItem("table").getNodeValue());
		this.setFunction(attributes.getNamedItem("function") != null  ? 
				attributes.getNamedItem("function").getNodeValue() : null);
		String type = attributes.getNamedItem("type") != null ? 
				attributes.getNamedItem("type").getNodeValue() : "string";
		this.setType(type);	
		this.setLabel(attributes.getNamedItem("label") != null ? attributes.getNamedItem("label").getNodeValue() : "noText");
		this.setDefaultValue(attributes.getNamedItem("default") != null ? attributes.getNamedItem("default").getNodeValue() : null);
		
		this.dimension.addProperty(this);
	}
	
	public void setLabel(String label){
		this.label = label;
	}
	
	public String getLabel(){
		return this.label;
	}

	public String getDefaultValue() {
		return defaultValue;
	}


	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	
	

}
